require('./index.css');
var _util = require('../../util/util.js');
var _account_service = require('service/account-service.js');
var _header = require('page/common/header/index.js');

var userInformation = {
    init : function(){
        $('#messageAccount').hide();
        $('#usernameTips').hide();
        $('#passwordTips').hide();
        $('#usernameTr').hide();
        $('#passwordTr').hide();
        this.bindEvents();
        this.loadAccountInfo();
        return this;
    },
    bindEvents : function(){
        var _this = this;
        $('#editAccount').click(
            function(){
                _this.editAccount();
            }
        );
        $('#repeatedPassword').blur(
            function(){
                $('#messageAccount').hide();
                $('#passwordTr').show();
                if(!($.trim($('#password').val()) == $.trim($('#repeatedPassword').val()))){
                    $('#passwordTips').css('color','red');
                    $('#passwordTips').show().text("两次密码不一致");
                }else{
                    $('#passwordTips').css('color','green');
                    $('#passwordTips').show().text("密码正确");
                }
            }
        );
    },
    loadAccountInfo : function(){
        _account_service.getAccountInformation(
            function(res){
                $('#username').text(res.username);
                $('#firstName').val(res.firstName);
                $('#lastName').val(res.lastName);
                $('#email').val(res.email);
                $('#phone').val(res.phone);
                $('#address1').val(res.address1);
                $('#address2').val(res.address2);
                $('#city').val(res.city);
                $('#state').val(res.state);
                $('#zip').val(res.zip);
                $('#country').val(res.country);
                if(res.languagePreference != ""){
                    $('#languagePreference').find("option:selected").text(res.languagePreference);
                }
                if(res.favouriteCategoryId != ""){
                    $('#favouriteCategoryId').find("option:selected").text(res.favouriteCategoryId);
                }
                if(res.bannerOption == true){
                    $('#bannerOption').attr("checked","checked");
                }
                if(res.listOption == true){
                    $('#listOption').attr("checked","checked");
                }
            },
            function(errMsg){
                $('#messageAccount').show().text(errMsg);
                $('#passwordTips').hide();
            }
        );
    },
    formValidation : function(){
        var result = {
            status : false,
            msg    : ''
        };
        //密码为空
            if(!($.trim($('#password').val()) == $.trim($('#repeatedPassword').val()))){
                result.msg = '两次密码不一致';
                return result;
            }
        //校验通过
        result.status = true;
        result.msg = '校验通过';
        return result;
    },
    editAccount : function(){
        var validationResult = this.formValidation();
        if(validationResult.status){
            var accountVO = {
                username : $('#username').text(),
                password : $.trim($('#password').val()),
                email    : $.trim($('#email').val()),
                firstName: $.trim($('#firstName').val()),
                lastName : $.trim($('#lastName').val()),
                status   : "",
                address1 : $.trim($('#address1').val()),
                address2 : $.trim($('#address2').val()),
                city     : $.trim($('#city').val()),
                state    : $.trim($('#state').val()),
                zip      : $.trim($('#zip').val()),
                country  : $.trim($('#country').val()),
                phone    : $.trim($('#phone').val()),
                favouriteCategoryId: $('#favouriteCategoryId').find('option:selected').val(),
                languagePreference : $('#languagePreference').find('option:selected').val(),
                listOption   :Boolean($('#listOption').prop("checked")),
                bannerOption :Boolean($('#bannerOption').prop("checked")),
                bannerName   : ""
            };
            console.log(accountVO);
            _account_service.editAccountInformation(accountVO,
                function(res){
                    window.location.href='./user-information.html';
                    _account_service.getAccountInformation();
                    $('#passwordTr').hide();
                    $('#passwordTips').hide();
                },
                function(errMsg){
                    $('#messageAccount').show().text(errMsg);
                    $('#passwordTr').hide();
                    $('#passwordTips').hide();
                });
        }else{
            $('#messageAccount').show().text(validationResult.msg);
            $('#passwordTr').hide();
            $('#passwordTips').hide();
        }
    }
};

module.exports = userInformation.init();